package com.ineatconseil.yougo.pages.requestsvalidation.view;

import java.util.List;

import org.apache.tapestry5.PersistenceConstants;
import org.apache.tapestry5.SelectModel;
import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.Log;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.corelib.components.BeanEditForm;

import com.ineatconseil.yougo.beans.ActionType;
import com.ineatconseil.yougo.beans.Request;
import com.ineatconseil.yougo.components.models.RequestTypeSelectModel;
import com.ineatconseil.yougo.dto.RequestDto;
import com.ineatconseil.yougo.exceptions.RestServiceException;
import com.ineatconseil.yougo.pages.AbstractPage;
import com.ineatconseil.yougo.pages.requestsvalidation.list.RequestsValidationList;
import com.ineatconseil.yougo.services.rest.RestPath;

public class RequestValidationView extends AbstractPage {

	@Property
	private Request request;

	@Property
	@Persist(PersistenceConstants.SESSION)
	private long userId;

	@Property
	@Persist(PersistenceConstants.SESSION)
	private long requestId;

	@Component(id = "formRequest")
	private BeanEditForm _form;

	@InjectPage
	private RequestsValidationList requestsList;

	/**
	 * Load action.
	 */
	@Log
	void onActivate(long requestId, long userId) {

		try {

			if (!isEmptyParameter(String.valueOf(requestId)) && !isEmptyParameter(String.valueOf(userId))) {

				List<RestPath> paths = RestPath.getSimplePath(RestPath.PATH_USERS, String.valueOf(userId));
				paths.add(new RestPath(RestPath.PATH_REQUESTS, String.valueOf(requestId)));

				request =
						this.getMapper().map(this.getRestServiceInvoker().call(paths, RequestDto.class), Request.class);

				// Save the id :
				this.requestId = requestId;
				this.userId = userId;
			} else {
				request = new Request();
				this.requestId = 0;
				this.userId = 0;
			}

		} catch (RestServiceException e) {
			_form.recordError(getRESTErrorMessage(e.getStatus()));
		}

	}

	/**
	 * Submit action.
	 */
	@Log
	Object onSuccess() {

		try {

			if (this.userId != 0 && this.requestId != 0) {

				List<RestPath> paths = RestPath.getSimplePath(RestPath.PATH_USERS, String.valueOf(userId));
				paths.add(new RestPath(RestPath.PATH_REQUESTS, String.valueOf(requestId)));

				// Update
				this.request.setId(this.requestId);
				this.getRestServiceInvoker()
						.update(paths, getMapper().map(request, RequestDto.class), RequestDto.class);

				requestsList.setActionMessageOk("request", ActionType.UPDATE);

			}
			return requestsList;

		} catch (RestServiceException e) {
			_form.recordError(getRESTErrorMessage(e.getStatus()));
			return null;
		}

	}

	public SelectModel getRequestTypesModel() {
		return (SelectModel) new RequestTypeSelectModel(this.getRestServiceInvoker());
	}

}
